const BaseRest = require('../base.js');
module.exports = class extends BaseRest {

    async placeAction(){
        let startAddress = this.post('startAddress'); //起点地址
        if(startAddress != 'nearby'){
            startAddress = JSON.parse(startAddress);
        }   
        let endAddress = JSON.parse(this.post('endAddress'));       //终点地址
        let weightValue = this.post('weight');        //重量
        let service_type = this.post('service_type');  //服务类型
        let tip = this.post('tip') || 0;  //小费
        
        let serviceConfig = await this.getSysConfig('service');
        if(serviceConfig.config_content){
            serviceConfig = JSON.parse(serviceConfig.config_content);
            let agent ;
            if(serviceConfig.open_agent === 1){
                let city = '';
                if(service_type == '帮我送' || service_type == '代驾'){
                    city = startAddress.city
                }else{
                    city = endAddress.city
                }
                //开启代理时
                agent = await this.model('agent').where({city_name: city}).find();
                if(!agent.city_name){
                    return this.fail(city+'还没有开通服务哦')
                }
                if(startAddress != 'nearby'){
                    if(startAddress.city != endAddress.city && agent.cross_city_service == 0){
                        return this.fail(startAddress.city+'不支持跨城服务')
                    }
                }
            }else{
                if(startAddress != 'nearby'){
                    if(startAddress.city != endAddress.city && serviceConfig.cross_city_service == 0 ){
                        return this.fail('不支持跨城服务')
                    }
                }
            }

            let distanceRule = await this.model('agent_distance_rules').where({agent_id: serviceConfig.open_agent === 1 ? agent.id:0,rule_type:service_type}).find();
            if(!distanceRule.id){
                return this.fail('路程计算规则未设置')
            }
            let distance ;
            if(startAddress == 'nearby'){
                distance = distanceRule.start_distance;
            }else{
                distance = await this.getDistance(startAddress.latitude+','+startAddress.longitude,endAddress.latitude+','+endAddress.longitude);
                if(distance.status != 0){
                    return this.fail('距离计算有误',distance)
                }
                distance = distance.result.elements[0].distance; //获得距离 单位(米)
            }
            if(distance > distanceRule.max_distance){
                return this.fail('超出最大服务距离'+distanceRule.max_distance/1000 + 'km');
            }
            let exccedDistance = 0; //超出起步路程部分
            let exccedPrice = 0; //超出起步路程部分计价 
            if(distance > distanceRule.start_distance){
                exccedDistance = parseInt(distance)-parseInt(distanceRule.start_distance);
                // 超出部分计价规则  (超出的距离/固定超出距离)向上取整 * 固定超出价格
                exccedPrice = parseInt(Math.ceil(exccedDistance/parseInt(distanceRule.exceed_everyone_distance)))*parseInt(distanceRule.exceed_everyone_price)
            }
            let totalPrice = parseFloat(exccedPrice) + parseFloat(distanceRule.start_price);
            let weightPrice = await this.getWeightPrice(0,weightValue);
            totalPrice += parseFloat(weightPrice);

            totalPrice += parseInt(tip); //小费
            let pay = {
                totalFee: totalPrice,
                out_trade_no: think.uuid('v4').replace(/-/g,"")
            }
           
            let order = await this.model('order').add({
                order_no: pay.out_trade_no,
                pay_amount: totalPrice,
                pay_type:1,
                wxcoupon_id:0,
                discount_amount:0,
                service_type:service_type,
                interal_value: 0,
                interal_amount: 0,
                night_price: 0,
                start_distance_amount:distanceRule.start_price,
                exceed_distance_amount:exccedPrice,
                exceed_distance:exccedDistance,
                weight_id:0,
                weight_price: 0,
                status: 1,
                start_address: this.post('startAddress'),
                end_address: this.post('endAddress'),
                goods_des: this.post('textarea'),
                send_time: new Date(),
                form_ids:'',
                wx_id: 0,
                openid: 0,
                order_type:1,
                opentp_key: this.post('app_key'),
                tip: this.post('tip') || 0,
                distance: distance
            });
            await this.model('order_ope').add({
                order_id: order
            })
            
            pay.order_id = order;
           
            return this.success(pay)
            
        }else{
            return this.fail('服务未配置')
        }
    }
}